import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../core/auth_store.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:flutter/foundation.dart' show defaultTargetPlatform, TargetPlatform, kIsWeb;
import 'package:url_launcher/url_launcher.dart';
import '../utils/util.dart';
import 'account_page.dart';
import 'settings_screen.dart';
import 'version_check_page.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({super.key});

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  int _selectedTab = 0;

  @override
  Widget build(BuildContext context) {
    final auth = Provider.of<AuthStore>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(auth.loggedIn ? (auth.username ?? '个人中心') : '个人中心'),
        toolbarHeight: 32,
        centerTitle:  true,
        backgroundColor: Colors.white.withOpacity(0.85), 
        //字体颜色
        titleTextStyle: TextStyle(fontSize: 18,color: Colors.black),
        elevation: 0,
      ),
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [Colors.white.withOpacity(0.85),Colors.white.withOpacity(0.85)],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 左侧纵向菜单
            Container(
              //设置当窄屏时宽度为80，宽屏时为120
              width: MediaQuery.of(context).size.width < 600 ? 80 : 120,
              padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(0.05),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  // _buildVerticalTab('账号', 0),
                  // const SizedBox(height: 4),
                  _buildVerticalTab('设置', 0),
                  const SizedBox(height: 4),
                  _buildVerticalTab('版本检测', 1),
                ],
              ),
            ),
            const VerticalDivider(width: 1),
            // 右侧内容区域
            Expanded(
              child: IndexedStack(
                index: _selectedTab,
                children: const [
                  // AccountPage(),
                  SettingsScreen(),
                  VersionCheckPage(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 纵向菜单项
  Widget _buildVerticalTab(String label, int index) {
    final selected = _selectedTab == index;
    return InkWell(
      onTap: () => setState(() => _selectedTab = index),
      borderRadius: BorderRadius.circular(8),
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 12),
        decoration: BoxDecoration(
          color: selected ? Colors.white10 : Colors.transparent,
          borderRadius: BorderRadius.circular(8),
        ),
        child: Text(
          label,
          style: TextStyle(
            fontWeight: selected ? FontWeight.w600 : FontWeight.normal,
            color: selected ? Colors.black.withOpacity(0.8) : Colors.black.withOpacity(0.9),
          ),
        ),
      ),
    );
  }
}
